package com.example.radiationarea.effect;

import com.example.radiationarea.RadiationAreaMod;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.client.gui.screens.inventory.EffectRenderingInventoryScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class RadiationEffect extends MobEffect {
    private static final Logger LOGGER = LogManager.getLogger();
    
    // Radiation damage source
    public static final DamageSource RADIATION_DAMAGE = new DamageSource("radiation") {
        @Override
        public Component getLocalizedDeathMessage(LivingEntity entityLivingBaseIn) {
            return new TextComponent(entityLivingBaseIn.getDisplayName().getString() + " died from radiation poisoning");
        }
    }.bypassArmor();
    
    public RadiationEffect() {
        super(MobEffectCategory.HARMFUL, 0xFF00FF00); // Green color
    }

    @Override
    public void applyEffectTick(LivingEntity entity, int amplifier) {
        // Apply radiation damage
        if (!entity.level.isClientSide) {
            // Calculate damage based on effect level, higher level = more damage
            float damage = (amplifier + 1) * 0.5f;
            entity.hurt(RADIATION_DAMAGE, damage);
            LOGGER.debug("Radiation effect applied to entity {}: amplifier={}, damage={}", 
                entity.getName().getString(), amplifier, damage);
        }
        super.applyEffectTick(entity, amplifier);
    }

    @Override
    public boolean isDurationEffectTick(int duration, int amplifier) {
        // Control effect application frequency - apply damage every 20 ticks (1 second)
        return duration % 20 == 0;
    }

    @Override
    @OnlyIn(Dist.CLIENT)
    public void initializeClient(java.util.function.Consumer<net.minecraftforge.client.EffectRenderer> consumer) {
        consumer.accept(new net.minecraftforge.client.EffectRenderer() {
            @Override
            public void renderInventoryEffect(MobEffectInstance effect, EffectRenderingInventoryScreen<?> screen, PoseStack poseStack, int x, int y, float z) {
                renderEffect(poseStack, x, y, z);
            }

            @Override
            public void renderHUDEffect(MobEffectInstance effect, GuiComponent gui, PoseStack poseStack, int x, int y, float z, float alpha) {
                renderEffect(poseStack, x, y, z);
            }
            
            private void renderEffect(PoseStack poseStack, int x, int y, float z) {
                RenderSystem.setShaderTexture(0, new ResourceLocation(RadiationAreaMod.MOD_ID, "textures/mob_effect/radiation.png"));
                GuiComponent.blit(poseStack, x + 3, y + 3, 0, 0, 18, 18, 18, 18);
            }
        });
    }
}